package com.example.bookshop.mapper;

import com.example.bookshop.entity.Cart;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @auth shanyue
 * @date 2025年06月04日16:12
 */
@Mapper
public interface CartMapper {
    @Select("SELECT * FROM cart WHERE user_id = #{userId}")
    List<Cart> findByUserId(int userId);

    @Select("SELECT id FROM users WHERE username = #{username}")
    Integer findUserIdByUsername(String username);

    @Select("SELECT * FROM cart WHERE user_id = #{userId} AND book_id = #{bookId}")
    Cart findByUserIdAndBookId(@Param("userId") int userId, @Param("bookId") int bookId);

    @Select("SELECT * FROM cart WHERE id = #{id}")
    Cart findById(int id);

    @Insert("INSERT INTO cart(user_id, book_id, quantity) VALUES(#{userId}, #{bookId}, #{quantity})")
    void insert(Cart cart);

    @Update("UPDATE cart SET quantity = #{quantity} WHERE id = #{id}")
    void update(Cart cart);

    @Delete("DELETE FROM cart WHERE id = #{id}")
    void deleteById(int id);

    @Delete("DELETE FROM cart WHERE user_id = #{userId}")
    void deleteByUserId(@Param("userId") Integer userId);
}
